package de.baumann.browser.model

import de.baumann.browser.api.net.DuoBaoOrderApiService
import de.baumann.browser.api.net.DuoBaoApiService
import de.baumann.browser.api.net.DuoBaoCommonApiService
import de.baumann.browser.api.net.HashApiService
import de.baumann.browser.api.net.base.ApiBuilder
import de.baumann.browser.api.net.vo.Result
import de.baumann.browser.api.net.vo.duobao.*
import de.baumann.browser.rx.RxSchedulers
import io.reactivex.Observable

private val duobaoApiService: DuoBaoApiService = ApiBuilder.buildService(DuoBaoApiService::class.java)
private val hashApiService: HashApiService = ApiBuilder.buildService(HashApiService::class.java)
private val orderApiService: DuoBaoOrderApiService = ApiBuilder.buildService(DuoBaoOrderApiService::class.java)
private val commonApiService: DuoBaoCommonApiService = ApiBuilder.buildService(DuoBaoCommonApiService::class.java)


fun selectHomeCarousel(): Observable<Result<List<Banner>>> {
    return duobaoApiService.selectHomeCarousel().compose(RxSchedulers.ioMain())
}

fun getDuoBaoMsg(): Observable<Result<DuoBaoMsg>> {
    return duobaoApiService.getDuoBaoMsg().compose(RxSchedulers.ioMain())
}

fun getDuoBaoList(): Observable<Result<List<DuoBao>>> {
    return duobaoApiService.getDuoBaoList().compose(RxSchedulers.ioMain())
}

fun getDuoBaoDetail(id: String): Observable<Result<DuoBao>> {
    return duobaoApiService.getDuoBaoDetail(id).compose(RxSchedulers.ioMain())
}

fun createOrder(id: String, number: String): Observable<Result<OrderResult>> {
    return duobaoApiService.createOrder(id, number).compose(RxSchedulers.ioMain())
}

fun payConfirm(key: String, id: String, number: String, hash: String, odin: String): Observable<Result<Any>> {
    return duobaoApiService.payConfirm(key, id, number, hash, odin).compose(RxSchedulers.ioMain())
}

fun checkHash(hash: String): Observable<Result<String>> {
    return hashApiService.check(hash, "-1")
            .compose(RxSchedulers.ioMain())
}

fun processOrder(): Observable<Result<List<DuoBao>>> {
    return orderApiService.process().compose(RxSchedulers.ioMain())
}

fun yiJieXiao(): Observable<Result<List<DuoBaoOrder>>> {
    return orderApiService.yiJieXiao().compose(RxSchedulers.ioMain())
}

fun selectAnnounceInfo(id: String): Observable<Result<DuoBao>> {
    return commonApiService.selectAnnounceInfo(id).compose(RxSchedulers.ioMain())
}

fun selectJoinList(id: String): Observable<Result<List<CYJL>>> {
    return commonApiService.selectJoinList(id).compose(RxSchedulers.ioMain())
}

fun selectAnnounceUserInfo(id: String): Observable<Result<ZJXX>> {
    return commonApiService.selectAnnounceUserInfo(id).compose(RxSchedulers.ioMain())
}


fun selectJoinInfo(id: String): Observable<Result<CYJL>> {
    return commonApiService.selectJoinInfo(id).compose(RxSchedulers.ioMain())
}

fun getOdinTreasureConfig(): Observable<Result<WFGZ>> {
    return commonApiService.getOdinTreasureConfig().compose(RxSchedulers.ioMain())
}

fun jiangpin(): Observable<Result<List<DuoBao>>> {
    return orderApiService.jiangpin().compose(RxSchedulers.ioMain())
}

fun wuliu(id: String): Observable<Result<JiangPin>> {
    return orderApiService.wuliu(id).compose(RxSchedulers.ioMain())
}

fun getAddress(): Observable<Result<Address>> {
    return orderApiService.getAddress().compose(RxSchedulers.ioMain())
}

fun sendSms(phone: String): Observable<Result<Any>> {
    if (phone.isEmpty()) {
        return Observable.just(Result("手机号码不能为空", -10))
    }
    return orderApiService.sendSms(phone).compose(RxSchedulers.ioMain())
}

fun yz(p: String, s: String): Observable<Result<Any>> {

    if (p.isEmpty()) {
        return Observable.just(Result("手机号码不能为空", -10))
    }

    if (s.isEmpty()) {
        return Observable.just(Result("验证码不能为空", -10))
    }

    return orderApiService.yz(p, s).compose(RxSchedulers.ioMain())
}


fun tjdz(n: String, p: String, a: String, i: String): Observable<Result<Any>> {
    if (n.isEmpty()) {
        return Observable.just(Result("姓名不能为空", -10))
    }
    if (p.isEmpty()) {
        return Observable.just(Result("手机号码不能为空", -10))
    }

    if (a.isEmpty()) {
        return Observable.just(Result("地址不能为空", -10))
    }

    if (i.isEmpty()) {
        return Observable.just(Result("详细地址不能为空", -10))
    }

    return orderApiService.tjdz(n, p, a, i).compose(RxSchedulers.ioMain())
}

fun dzgx(n: String, p: String, a: String, i: String): Observable<Result<Any>> {
    if (n.isEmpty()) {
        return Observable.just(Result("姓名不能为空", -10))
    }
    if (p.isEmpty()) {
        return Observable.just(Result("手机号码不能为空", -10))
    }

    if (a.isEmpty()) {
        return Observable.just(Result("地址不能为空", -10))
    }

    if (i.isEmpty()) {
        return Observable.just(Result("详细地址不能为空", -10))
    }

    return orderApiService.dzgx(n, p, a, i).compose(RxSchedulers.ioMain())
}